package com.itbaizhan.openhis.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itbaizhan.openhis.domain.CheckItem;
import com.itbaizhan.openhis.dto.CheckItemDto;
import com.itbaizhan.openhis.service.CheckItemService;
import com.itbaizhan.openhis.mapper.CheckItemMapper;
import com.itbaizhan.openhis.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
* @author MR
* @description 针对表【sys_check_item(检查费用表)】的数据库操作Service实现
* @createDate 2025-07-02 21:56:07
*/
@Service
public class CheckItemServiceImpl extends ServiceImpl<CheckItemMapper, CheckItem>
    implements CheckItemService{
	
	@Autowired
	private CheckItemMapper checkItemMapper;
	
	/**
	 * 分页查询检查项目
	 * @param checkItemDto
	 * @return
	 */
	@Override
	public DataGridView listCheckItemForPage(CheckItemDto checkItemDto) {
		Page<CheckItem> page = new Page<>(checkItemDto.getPageNum(), checkItemDto.getPageSize());
		QueryWrapper<CheckItem> wrapper = new QueryWrapper<>();
		wrapper.like(StringUtils.isNotBlank(checkItemDto.getCheckItemName()),
		CheckItem.COL_CHECK_ITEM_NAME, checkItemDto.getCheckItemName());
		wrapper.like(StringUtils.isNotBlank(checkItemDto.getKeywords()),
				CheckItem.COL_KEYWORDS, checkItemDto.getKeywords());//关键字
		wrapper.eq(StringUtils.isNotBlank(checkItemDto.getTypeId()),
				CheckItem.COL_TYPE_ID, checkItemDto.getTypeId());
		checkItemMapper.selectPage(page, wrapper);
		return new DataGridView(page.getTotal(), page.getRecords());
	}
	
	/**
	 * 添加检查项目
	 * @param checkItemDto
	 * @return
	 */
	@Override
	public int addCheckItem(CheckItemDto checkItemDto) {
		CheckItem checkItem = new CheckItem();
		BeanUtils.copyProperties(checkItemDto, checkItem);
		checkItem.setCreateBy(checkItemDto.getSimpleUser().getUserName());
		checkItem.setCreateTime(DateUtil.date());
		return checkItemMapper.insert(checkItem);
	}
	
	/**
	 * 修改检查项目
	 * @param checkItemDto
	 * @return
	 */
	@Override
	public int updateCheckItem(CheckItemDto checkItemDto) {
		CheckItem checkItem = new CheckItem();
		BeanUtils.copyProperties(checkItemDto, checkItem);
		checkItem.setUpdateBy(checkItemDto.getSimpleUser().getUserName());
		checkItem.setUpdateTime(DateUtil.date());
		return checkItemMapper.updateById(checkItem);
	}
	
	/**
	 * 根据ID查询检查项目
	 * @param checkItemId
	 * @return
	 */
	@Override
	public CheckItem getOne(Long checkItemId) {
		return checkItemMapper.selectById(checkItemId);
	}
	
	/**
	 * 删除检查项目
	 * @param checkItemIds
	 * @return
	 */
	@Override
	public int deleteCheckItemByIds(Long[] checkItemIds) {
		List<Long> ids = Arrays.asList(checkItemIds);
		if (ids.size() > 0&& ids != null){
			return checkItemMapper.deleteBatchIds(ids);
		}
		return 0;
	}
}




